<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="代码" prop="code">
      <el-input v-model="dataForm.code" placeholder="代码"></el-input>
    </el-form-item>
    <el-form-item label="转换类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="转换类型"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="来源模板的代码" prop="sourceCode">
      <el-input v-model="dataForm.sourceCode" placeholder="来源模板的代码"></el-input>
    </el-form-item>
    <el-form-item label="来源模板的名称" prop="sourceName">
      <el-input v-model="dataForm.sourceName" placeholder="来源模板的名称"></el-input>
    </el-form-item>
    <el-form-item label="表名" prop="targetCode">
      <el-input v-model="dataForm.targetCode" placeholder="表名"></el-input>
    </el-form-item>
    <el-form-item label="目标模板的代码" prop="targetName">
      <el-input v-model="dataForm.targetName" placeholder="目标模板的代码"></el-input>
    </el-form-item>
    <el-form-item label="目标模板的名称" prop="parentName">
      <el-input v-model="dataForm.parentName" placeholder="目标模板的名称"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="是否可重复" prop="canRepeat">
      <el-radio v-model="dataForm.canRepeat" label="1">是</el-radio>
      <el-radio v-model="dataForm.canRepeat" label="0">否</el-radio>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/def/convert'

    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          type: '',
          name: '',
          sourceCode: '',
          sourceName: '',
          targetCode: '',
          targetName: '',
          parentName: '',
          description: '',
          canRepeat: '',
          status: ''
        },
        dataRule: {
          code: [
            { required: true, message: '代码不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '转换类型不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          sourceCode: [
            { required: true, message: '来源模板的代码不能为空', trigger: 'blur' }
          ],
          sourceName: [
            { required: true, message: '来源模板的名称不能为空', trigger: 'blur' }
          ],
          targetCode: [
            { required: true, message: '表名不能为空', trigger: 'blur' }
          ],
          targetName: [
            { required: true, message: '目标模板的代码不能为空', trigger: 'blur' }
          ],
          parentName: [
            { required: true, message: '目标模板的名称不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          canRepeat: [
            { required: true, message: '是否可重复不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
